package com.tushu.view.student;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.tushu.dao.BookDao;
import com.tushu.model.Book;
import com.tushu.model.Student;
import com.tushu.utils.DataUtils;

import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LendBook extends JPanel {
	private JTextField isbn_text;
	private JTable table;
	
	BookDao bookDao=new BookDao();
	

	Student student;
	
	String[] headString={"序号","ID","ISBN","书名","作者","借出时间","应还时间","状态"};
	
	Object[][] dataObjects=null;
	
	String sql="SELECT\r\n"
			+ "	l.id,\r\n"
			+ "	b.isbn,\r\n"
			+ "	b.book_name,\r\n"
			+ "	b.author,\r\n"
			+ "	l.lend_time,\r\n"
			+ "	l.return_time, \r\n"
			+ "	l.status \r\n"
			+ "FROM\r\n"
			+ "	tb_student s\r\n"
			+ "	LEFT JOIN tb_lend l ON s.id = l.student_id\r\n"
			+ "	LEFT JOIN tb_book b ON b.id = l.book_id \r\n"
			+ "WHERE\r\n"
			+ "	s.account =?";

	/**
	 * Create the panel.
	 * @throws SQLException 
	 */
	public LendBook(Student student) throws SQLException {
		setLayout(null);
		this.student=student;
		JButton btnNewButton = new JButton("\u786E\u8BA4");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String isbn=isbn_text.getText();
				Book book=bookDao.getBookByIsbn(isbn);
				if (null==book) {
					JOptionPane.showMessageDialog(null, "查无此书!");
				}else if (0==book.getNum()) {
					JOptionPane.showMessageDialog(null, "该书暂时无库存!");
				}
				else {
	                int i1 = bookDao.addRecord(student.getId(), book.getId());
	                int i2=bookDao.updateBook1(book.getNum() - 1, book.getIsbn());
	                if (i1>0&&i2>0) {
	                	JOptionPane.showMessageDialog(null, "借阅《"+book.getBookName()+"》成功!");
	                	isbn_text.setText("");
	                	
	                	try {
							getMyLend();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}else {
						JOptionPane.showMessageDialog(null, "操作失败!");
					}
				}
			}
		});
		btnNewButton.setBounds(307, 24, 97, 23);
		add(btnNewButton);
		
		JLabel lblNewLabel_1 = new JLabel("\u8F93\u5165\u8981\u501F\u7684\u4E66\u7C4D\u7684ISBN\uFF1A");
		lblNewLabel_1.setBounds(21, 32, 168, 15);
		add(lblNewLabel_1);
		
		isbn_text = new JTextField();
		isbn_text.setBounds(170, 25, 108, 21);
		add(isbn_text);
		isbn_text.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 67, 504, 199);
		add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		table.setModel(new DefaultTableModel(dataObjects,headString));
		
		JButton btnNewButton_1 = new JButton("\u6211\u8981\u8FD8\u4E66");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row=table.getSelectedRow();
				if (-1 == row) {
					JOptionPane.showMessageDialog(null, "请选择一行");
					return;
				} else {
					String status=(String) table.getValueAt(row,7);
					String isbn=(String)table.getValueAt(row, 2);
					if ("已还".equals(status)) {
						JOptionPane.showMessageDialog(null,"该书已还！");
					}else if (row!=-1) {
						int lend_id=(int) table.getValueAt(row, 1);
						int res=bookDao.updateRecord(lend_id);
						Book book=bookDao.getBookByIsbn(isbn);
						int res2=bookDao.updateBook1(book.getNum()+1, isbn);
						if(res>0&&res2>0) {
							JOptionPane.showMessageDialog(null,"还书成功！");
							try {
								getMyLend();
							} catch (SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						}else {
							JOptionPane.showMessageDialog(null,"操作失败！");
						}
					}
				}
			}
		});
		btnNewButton_1.setBounds(396, 276, 97, 23);
		add(btnNewButton_1);
		
		JLabel lblNewLabel = new JLabel("\u63D0\u793A\uFF1A\u9009\u4E2D\u76F8\u5E94\u7684\u501F\u4E66\u8BB0\u5F55\uFF0C\u53EF\u64CD\u4F5C\u5F52\u8FD8");
		lblNewLabel.setBounds(134, 280, 297, 15);
		add(lblNewLabel);
		getMyLend();
		

	}
	public void getMyLend() throws SQLException {
		ResultSet set=DataUtils.query(sql, new Object[] {student.getAccount()});
		table.setModel(new DefaultTableModel(DataUtils.getSetArrays(set),headString));
	}
}
